package Jasper.dao.impl;



import Jasper.dao.IUserDao;
import Jasper.entity.Product;
import Jasper.entity.User;
import Jasper.utils.DBManager;

import java.util.List;

/**
 *
 */
public class UserDaoImpl implements IUserDao {

    //

    /**
     * 查询  查询所有用户的总条数  返回值是总条数  getPage
     * 重写方法  --查询所有用户的总条数  返回值是总条数
     * @return
     */
    @Override
    public int getTotalCount() {
        /*Connection connection = null;
        Statement statement = null;
        //返回结果数组
        ResultSet rSet = null;
        //返回值 获取总数据条数
        int num = 0;
        try {
            connection = DBUtils.getConnection();
            statement = connection.createStatement();
            rSet = statement.executeQuery(
                    "select count(*) from user where is_delete = 1");
            if(rSet.next()){
                num = rSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
                //关闭结果数组，关闭创建，关闭连接
            DBUtils.closeAll(rSet,statement,connection);
        }*/
//       Integer rSet = DBManager.commonQuery("select count(*) from user where is_delete = 1",User.class);
        int count = DBManager.commonCount("select count(*) from user where is_delete = 1");
       return count;
    }

    /**
     * 分页请求  查询
     * @param offset 偏移量
     * @param num    数据量
     * @return
     */
    @Override
    public List<User> getList(int offset, int num) {
        List<User> list = DBManager.commonQuery("select * from user where is_delete = 1 limit ?,?;",
                User.class,
                offset,num);
        return list;
    }

    /**
     * 插入  插入用户信息
     * @param user 添加的用户信息
     * @return
     */
    @Override
    public int addUser(User user) {
        Integer rSet = DBManager.commonUpdate(
                "insert into user(username,password,sex,hobbiesString,addrs,imgUrl) values(?,?,?,?,?,?)",
                user.getUsername(),
                user.getPassword(),
                user.getSex(),
                user.getHobbiesString(),
                user.getAddrs(),
                user.getImgUrl());
        return rSet;
    }

    /**
     * 更新
     * @param user 修改的用户信息
     * @return
     */
    @Override
    public int updateUser(User user) {
        Integer rSet = DBManager.commonUpdate("update user set username=?,password=?,sex=?,hobbiesString=?,addrs=?,imgUrl=? where id =?",
                user.getUsername(),
                user.getPassword(),
                user.getSex(),
                user.getHobbiesString(),
                user.getAddrs(),
                user.getImgUrl(),
                user.getId());
        return rSet;
    }

    /**
     * 删除元素
     * @param id 要删除的id
     * @return
     */
    @Override
    public int deleteUser(int id) {
        Integer rSet = DBManager.commonUpdate("update user set is_delete = 0 where id = ?", id);
        return rSet;
    }

    /**
     * 查询展示所有数据，返回为数组
     * @return
     */
    @Override

    public List<User> getList() {
        List<User> list = DBManager.commonQuery("select * from user where is_delete = 1",
                User.class);
        return list;
    }

    /**
     * 查询账号和密码  登录页面
     * @param user
     * @return
     */
    @Override
    public int checkUser(User user) {
        int count = DBManager.commonCount("select userid from user where username=? and password=? and is_delete=1",
                user.getUsername(),user.getPassword());
        return count;
    }

    /**
     * 查询是否有相同用户
     *
     * @param
     * @param username
     * @return
     */
    @Override
    public int checkName(String username) {
        int num=0;
        num = DBManager.commonCount("select count(*) from user where username=?", username);
        return num;
    }

    /**
     * 分页商品页面的信息
     *
     * @param offset
     * @param num
     * @return
     */

    @Override
    public List<Product> getproductlist(int offset, int num) {
        List<Product> list = DBManager.commonQuery("select * from product where STATUS = 1 limit ?,?;",
                Product.class,
                offset, num);
        return list;
    }

    /**
     * 商品上架  返回操作数
     *
     * @param product
     * @return
     */
    @Override
    public int addProduct(Product product) {
        Integer rSet = DBManager.commonUpdate(
                "insert into product(class_id,name,detail,price,stock,imgUrl) values(?,?,?,?,?,?)",
                product.getClass_id(),
                product.getName(),
                product.getDetail(),
                product.getPrice(),
                product.getStock(),
                product.getImgUrl());
        return rSet;
    }



}
